package com.isoftstone.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.isoftstone.entity.Menu;
import com.isoftstone.entity.Role;
import com.isoftstone.entity.Users;
import com.isoftstone.service.RoleService;

@Controller
@RequestMapping("roles")
public class RoleController {
	
	@Autowired
	private RoleService roleService;

	@RequestMapping("roleList")
	public String getAllRoles(ModelMap modelMap){
	// TODO Auto-generated method stub
				
		List<Role> roleList=roleService.getAllRoleList();
		List<Menu> list = roleService.getAllMenuList();
		modelMap.addAttribute("roleList",roleList);
		modelMap.addAttribute("menuList",list);
		return "RoleList";		
	}
	@RequestMapping("insertRole")
	@ResponseBody
	public boolean insertUser(String role_id,String role_name){
			Role role=new Role();
			role.setRole_id(role_id);
			role.setRole_name(role_name);
			
			boolean flag = roleService.insertRole(role);
			return flag;	
	}
	@RequestMapping("getRoleByRoleId")
	@ResponseBody
	public Role getRoleByRoleId(String role_id){
		Role role = roleService.getRoleByRoleId(role_id);
		return role;
	}
	
	@RequestMapping("editRole")
	@ResponseBody
	public boolean editUser(String role_id,String role_name){
		
			

			Role role=new Role();
			role.setRole_id(role_id);
			role.setRole_name(role_name);
			
			boolean flag = roleService.editRole(role);
			return flag;		
	}
	
	@RequestMapping("deleteRole")
	@ResponseBody
	public boolean deletUser(String role_id){
		
			
			boolean flag = roleService.deleteRole(role_id);
			return flag;		
	}
	@RequestMapping("getMenuListByRoleId")
	@ResponseBody
	public List<Menu> getMenuListByRoleId(String role_id){
		List<Menu> menuList=roleService.getMenuListByRoleId(role_id);
		return menuList;
	}
	@RequestMapping("editRoleMenu")
	@ResponseBody
	public boolean editRoleMenu(String role_id,String menus){
		String str=menus.endsWith("|")?menus.substring(0,menus.length()-1):menus;
		String[] str1 = str.split("\\|");
		String[] menuIds = new String[str1.length];
		for(int i=0;i<str1.length;i++){
			menuIds[i] = String.valueOf(str1[i]);
		}
		boolean flag = roleService.editRoleMenu(role_id,menuIds);
		return flag;
		
	}
}
